package
{
  import flash.display.MovieClip;
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  import flash.events.NetStatusEvent;
  import flash.events.SecurityErrorEvent;
  import flash.media.Video;
  import flash.net.NetConnection;
  import flash.net.NetStream;
  import flash.text.TextField;
  
  import org.osmf.containers.MediaContainer;
  import org.osmf.media.DefaultMediaFactory;
  import org.osmf.media.MediaElement;
  import org.osmf.media.MediaFactory;
  import org.osmf.media.MediaPlayer;
  import org.osmf.media.URLResource;
  
  public class VideoAS3 extends Sprite
  {
    private var videoURL:String = "1"; 
    private var connection:NetConnection; 
    private var stream:NetStream; 
    private var currentVideo:String = ""; 
    private var streams:Array;
    private var spacesY:Number = 10;
    private var video:Video;
    private var media:MovieClip = new MovieClip();
    
    public function VideoAS3()
    {
      addChild(media).x = 90;
      load();
      createList();
      
    }
    
    private function createList():void
    {
      for (var i:int = 0; i < streams.length; i++) 
      {
        var mc:MovieClip = new MovieClip();
        var text:TextField = new TextField();
        mc.addChild(text);
        text.text = (streams[i] as StreamModel).name;
        mc.x = 10;
        addChild(mc).y = spacesY;
        spacesY += 12;
        mc["model"] = streams[i];
        mc.addEventListener(MouseEvent.CLICK, onClick);
        mc.mouseChildren = false;
        mc.mouseEnabled = true;
        mc.buttonMode = true;
      }
      
    }
    
    protected function onClick(event:MouseEvent):void
    {
      trace(((event.target as MovieClip)["model"] as StreamModel).name);
      var model:StreamModel = (event.target as MovieClip)["model"] as StreamModel;
      currentVideo = model.streamName;
//      if(currentVideo.substring(currentVideo.length-3, currentVideo.length) == "f4m" ||
//        currentVideo.substring(currentVideo.length-3, currentVideo.length) == "flv"){
        connetToF4MStream(model.url);
//      }else{
//        connetToStream(model.url);
//      }
    }
    
    /**
     * connetToStream is function
     * @author Aleksandar Bosancic
     * @date Nov 7, 2013
     */
    private function connetToStream(url:String = "rtmp://79.175.124.178:1935/stream/"):void
    {
		if(connection){
			connection.close();
			connection = null;
		}
		connection = new NetConnection(); 
		connection.objectEncoding = flash.net.ObjectEncoding.AMF3; 
		connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
		connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		connection.client = new CustomClient();
		connection.connect(url);
    }
    
    private function netStatusHandler(event:NetStatusEvent):void
    {
      switch (event.info.code)
      {
        case "NetConnection.Connect.Success":
          connectStream();
          break;
        case "NetStream.Play.StreamNotFound":
          trace("Stream not found: " + videoURL);
		  break;
		case "NetConnection.Connect.Closed":
		  trace("Stream close: " + videoURL);
          break;
      }
    }
    
    private function securityErrorHandler(event:SecurityErrorEvent):void
    {
      trace("securityErrorHandler: " + event);
    }
    
    private function connectStream():void
    {
      var stream:NetStream = new NetStream(connection);
      stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
      stream.client = new CustomClient();
	  if(video){
	  	video.clear();
		video = null;
	  }
      video = new Video();
      video.attachNetStream(stream);
      stream.play(currentVideo);
      media = new MovieClip();
      addChild(media).x = 90;
      media.addChild(video);
    }
    
    /**
     * connetFLVStream is function
     * @author Aleksandar Bosancic
     * @date Nov 7, 2013
     */
    private function connetToF4MStream(url:String = "http://4.31.30.135:1935/st3fstv/3ios_OBN_1237865/manifest.f4m"):void
    {
      var mediaFactory:MediaFactory = new DefaultMediaFactory();
      
      //var resource:URLResource = new URLResource( "http://4.31.30.135:1935/st3fstv/3ios_OBN_1237865/manifest.f4m" );
      var resource:URLResource = new URLResource( url );
      
      var element:MediaElement = mediaFactory.createMediaElement( resource );
      
      var mediaPlayer:MediaPlayer = new MediaPlayer( element );
      
      var mediaContainer:MediaContainer = new MediaContainer();
      mediaContainer.addMediaElement( element );
      media = new MovieClip();
      addChild(media).x = 90;
      media.addChild( mediaContainer );
    }
    
    /**
     * load is function
     * @author Aleksandar Bosancic
     * @date Nov 7, 2013
     */
    private function load():void
    {
     streams = new Array();
     streams.push(new StreamModel("rtmp://79.175.124.178:1935/stream/","b92.stream", "live B92 TV"));
     streams.push(new StreamModel("rtmp://92.60.231.48:1935/live/","livestream", "NTV"));
     streams.push(new StreamModel("rtmp://79.175.124.178:1935/stream/","rts1.stream", "RTRS1"));
     streams.push(new StreamModel("rtmp://79.175.124.178:1935/stream/","rts2.stream", "RTRS2"));
     streams.push(new StreamModel("rtmp://79.175.124.178:1935/stream/","rtsdigital.stream", "Digital"));
     streams.push(new StreamModel("rtmp://79.175.124.178:1935/stream/","studiob.stream", "StudioB"));
     streams.push(new StreamModel("rtmp://cp39414.live.edgefcs.net/live/","lcn1@9753", "lcn1@9753"));
	   streams.push(new StreamModel("rtmp://91.121.9.48/live/","channel4", "sport"));
	 
     
     streams.push(new StreamModel("http://4.31.30.135:1935/st3fstv/4ios_Federalna_8795632/manifest.f4m","manifest.f4m", "FTV"));
     streams.push(new StreamModel("http://www.youtube.com/watch?v=UqSww10eeKw","UqSww10eeKw", "UqSww10eeKw"));
//     streams.push(new StreamModel("http://www.sportsembed.com/discoverytv.stream.flv","discoverytv.stream.flv", "DISCOVERY"));
     
     //streams.push(new StreamModel("http://4.31.30.135:1935/st3fstv/3ios_OBN_1237865/manifest.f4m","manifest.f4m", "OBN TV"));
     
    }
    
  }
}